package view.microgui
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import model.macro.Unit;
	import model.micro.FieldUnit;
	
	/**
	 *
	 */
	public class FieldUnitListItem extends Sprite
	{
		static public const HEIGHT:Number = 34;
		static public const WIDTH:Number = 120;
		static private const PADDING:Number = 4;
		static private const PASSIVE_ALPHA:Number = 0.7;
		static private const ACTIVE_ALPHA:Number = 0.9;
		
		public var unit:Unit;
		public var filter:Boolean;
		
		private var name_field:TextField;
		private var health_bar_back:Shape;
		private var health_bar_front:Shape;
		private var morale_bar_back:Shape;
		private var morale_bar_front:Shape;
		
		public function FieldUnitListItem(unit:Unit)
		{
			if (unit == null)
			{
				throw new ArgumentError("unit is null");
			}
			this.unit = unit;
			
			filter = true;
			alpha = PASSIVE_ALPHA;
			mouseChildren = false;
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			
			// draw background
			graphics.beginFill(0xBBBBBB);
			graphics.lineStyle(1, 0x888888);
			graphics.drawRoundRect(0, 0, WIDTH, HEIGHT, 8, 8);
			graphics.endFill();
			
			// name
			name_field = new TextField();
			name_field.defaultTextFormat = new TextFormat("Helvetica", 12, 0x000000);
			name_field.text = unit.name;
			name_field.height = 20;
			name_field.width = WIDTH - 2 * PADDING;
			name_field.x = PADDING;
			name_field.y = 0;
			addChild(name_field);
			
			// health bar
			health_bar_back = new Shape();
			health_bar_back.graphics.beginFill(0x444444);
			health_bar_back.graphics.drawRect(0, 0, WIDTH - PADDING * 2, 6);
			health_bar_back.graphics.endFill();
			health_bar_back.x = PADDING;
			health_bar_back.y = HEIGHT - health_bar_back.height - PADDING;
			addChild(health_bar_back)
			
			health_bar_front = new Shape();
			health_bar_front.graphics.beginFill(0x00FF00);
			health_bar_front.graphics.drawRect(0, 0, health_bar_back.width, health_bar_back.height);
			health_bar_front.graphics.endFill();
			health_bar_front.x = health_bar_back.x;
			health_bar_front.y = health_bar_back.y;
			addChild(health_bar_front)
			
			// morale bar
			morale_bar_back = new Shape();
			morale_bar_back.graphics.beginFill(0x444444);
			morale_bar_back.graphics.drawRect(0, 0, WIDTH - PADDING * 2, 6);
			morale_bar_back.graphics.endFill();
			morale_bar_back.x = PADDING;
			morale_bar_back.y = health_bar_back.y - morale_bar_back.height;
			addChild(morale_bar_back)
			
			morale_bar_front = new Shape();
			morale_bar_front.graphics.beginFill(0xFFFFFF);
			morale_bar_front.graphics.drawRect(0, 0, morale_bar_back.width, morale_bar_back.height);
			morale_bar_front.graphics.endFill();
			morale_bar_front.x = morale_bar_back.x;
			morale_bar_front.y = morale_bar_back.y;
			addChild(morale_bar_front)
		}
		
		private function onMouseOut(e:MouseEvent):void 
		{
			alpha = PASSIVE_ALPHA;
		}
		
		private function onMouseOver(e:MouseEvent):void 
		{
			alpha = ACTIVE_ALPHA;
		}
		
		public function update():void
		{
			health_bar_front.scaleX = (1.0 * unit.number_of_soldiers) / unit.getMaxSoldiers();
			morale_bar_front.scaleX = unit.morale / Unit.MAX_MORALE;
		}
	
	}

}